package com.sixmac.controller;

import com.sixmac.annotation.AuthUser;
import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.module.Banner;
import com.sixmac.module.Installer;
import com.sixmac.module.Landlord;
import com.sixmac.platform.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by admin on 2018/9/5.
 */
@Controller
@RequestMapping("/index")
@ResponseBody
public class IndexController extends BaseController {
    @Autowired
    IndexService indexService;

    /**
     * @api {post} /index/recordRoom 0.1录入房屋信息
     * @apiName index.index
     * @apiGroup index
     * @apiParam {String} token token
     * @apiParam {String} lng 经度
     * @apiParam {String} lat 纬度
     * @apiParam {String} address 详细地址 比如 武汉光谷科技2楼1902
     * @apiParam {String} region 房屋区域 areaCode的值
     * @apiParam {String} images 房源图片 多个用,分隔
     * @apiParam {String} mobile 房东手机
     * @apiParam {String} lockNo 锁的编号
     * @apiSuccess {String} user.id 安装员id
     * @apiSuccess {String} user.token token
     * @apiSuccess {String} user.name 名称
     * @apiSuccess {String} user.sex 性别 1男2女
     */
    @AuthUser
    @RequestMapping("/recordRoom")
    public AjaxResult recordRoom(Installer installer,String lng, String lat, String address, Integer region, String images, String mobile, String lockNo,String roomNum) {
        return indexService.recordRoom(installer,lng, lat, region, address, images, mobile, lockNo, roomNum);
    }


    @RequestMapping("/banner")
    public AjaxResult recordRoom() {
        List<Banner> banners = Blade.create(Banner.class).findBy(" type =1",null);
        return json(banners);
    }

    @AuthUser
    @RequestMapping("/logout")
    public AjaxResult logout(Installer installer) {
        Landlord landlord = Blade.create(Landlord.class).findFirstBy("wechatKey='"+installer.getWechatKey()+"'", null);
        if (landlord!=null){
            landlord.setWechatKey("");
            Blade.create(Landlord.class).updateEveryCol(landlord);
        }
        installer = Blade.create(Installer.class).findById(installer.getId());
        installer.setWechatKey("");
        Blade.create(Installer.class).updateEveryCol(installer);
        return success("退出成功");
    }
}
